# -*- coding:UTF-8 -*-
from django.db.models import Q
from cmdb.models import EquipmentInfo

import string

#服务器列表
def equipment_info(start,end,serverRoomId,equipmentType,searchStr):
    count = 0
    equipmentRes = {}
    if searchStr == '':
        if serverRoomId == 0:
            if equipmentType == 0:
                count=EquipmentInfo.objects.count()
                equipmentRes = EquipmentInfo.objects.order_by('-id')[start:end]
            else:
                count=EquipmentInfo.objects.filter(equipment_type=equipmentType).count()
                equipmentRes = EquipmentInfo.objects.filter(equipment_type=equipmentType).order_by('-id')[start:end]
        else:
            if equipmentType == 0:
                count=EquipmentInfo.objects.filter(server_room_id=serverRoomId).count()
                equipmentRes = EquipmentInfo.objects.filter(server_room_id=serverRoomId).order_by('-id')[start:end]
            else:
                count=EquipmentInfo.objects.filter(Q(server_room_id=serverRoomId),Q(equipment_type=equipmentType)).count()
                equipmentRes = EquipmentInfo.objects.filter(Q(server_room_id=serverRoomId),Q(equipment_type=equipmentType)).order_by('-id')[start:end]
    else:
        if serverRoomId == 0:
            if equipmentType == 0:
                count = EquipmentInfo.objects.filter(Q(serial_num__icontains=searchStr)| Q(sys_ip_inner__icontains=searchStr)).count()
                equipmentRes = EquipmentInfo.objects.filter(Q(serial_num__icontains=searchStr)| Q(sys_ip_inner__icontains=searchStr)).order_by('-id')[start:end]
            else:
                count = EquipmentInfo.objects.filter(Q(equipment_type=equipmentType),Q(serial_num__icontains=searchStr)| Q(sys_ip_inner__icontains=searchStr)).count()
                equipmentRes = EquipmentInfo.objects.filter(Q(equipment_type=equipmentType),Q(serial_num__icontains=searchStr)| Q(sys_ip_inner__icontains=searchStr)).order_by('-id')[start:end]
        else:
            if equipmentType == 0:
                count = EquipmentInfo.objects.filter(Q(server_room_id=serverRoomId),Q(serial_num__icontains=searchStr)| Q(sys_ip_inner__icontains=searchStr)).count()
                equipmentRes = EquipmentInfo.objects.filter(Q(server_room_id=serverRoomId),Q(serial_num__icontains=searchStr)| Q(sys_ip_inner__icontains=searchStr)).order_by('-id')[start:end]
            else:
                count = EquipmentInfo.objects.filter(Q(equipment_type=equipmentType),Q(server_room_id=serverRoomId),Q(serial_num__icontains=searchStr)| Q(sys_ip_inner__icontains=searchStr)).count()
                equipmentRes = EquipmentInfo.objects.filter(Q(equipment_type=equipmentType),Q(server_room_id=serverRoomId),Q(serial_num__icontains=searchStr)| Q(sys_ip_inner__icontains=searchStr)).order_by('-id')[start:end]
    return {'count':count,'equipment_info_list':equipmentRes}

#获取最大服务器编号
def get_max_equipment_no(cursor):
    equipment_no = 'SX00001'
    count_sql = "select equipment_no from equipment_info order by equipment_no desc limit 1";
    cursor.execute(count_sql)
    eqiupmentRes = cursor.fetchone()
    if eqiupmentRes:
            tmpNo=str(int(eqiupmentRes[0][2:]) + 1)
            equipment_no = 'SX' + string.rjust(tmpNo,5,'0')
    return equipment_no

def add_equipment_info(equipment_no, equipment_type, equipment_model, hostname, serial_num, asset_number, cpu_model, \
                       cpu, memory, harddisk, sys_ip_inner, sys_ip_outer, environment, server_room_id, room_no_id, \
                       cabinet, idrac, occupy_u, start_cabinet, end_cabinet, start_time, maint_end_time, manufacturer_id, \
                       provider_id, comment, operator, responser, cursor):
    equipment_sql = 'insert into equipment_info set equipment_no="' + equipment_no + '",equipment_type='  \
                        + str(equipment_type) + ',equipment_model="' + equipment_model + '",hostname="' + hostname \
                        + '",serial_num="' + serial_num + '",asset_number="' + asset_number + '",cpu_model="' + cpu_model \
                        + '",cpu="' + cpu + '",memory="' + memory + '",harddisk="' + harddisk \
                        + '",sys_ip_inner="' + sys_ip_inner + '",sys_ip_outer="' + sys_ip_outer + '",environment=' + str(environment) \
                        + ',server_room_id=' + str(server_room_id) + ',room_no_id=' + room_no_id + ',cabinet="' + cabinet + '",idrac="' + idrac \
                        + '",occupy_u=' + str(occupy_u) + ',start_cabinet=' + str(start_cabinet)  + ',end_cabinet=' + str(end_cabinet) \
                        + ',start_time="' + start_time + '",maint_end_time="' + maint_end_time + '",manufacturer_id=' + str(manufacturer_id) \
                        +',provider_id=' + str(provider_id) + ',comment="' + comment + '",operator="' + operator + '",responser="' + responser + '"'
    cursor.execute(equipment_sql)

def get_equipment_info(equipment_no):
    equipmentRes = EquipmentInfo.objects.get(equipment_no=equipment_no)
    return equipmentRes

def get_eno_equipment_info(sys_ip_inner):
    equipmentRes = EquipmentInfo.objects.filter(sys_ip_inner=sys_ip_inner)
    if equipmentRes:
        for singleV in equipmentRes:
            return singleV.equipment_no
    else:
        return False

def edit_equipment_info(equipment_no, equipment_type, equipment_model, hostname, serial_num, asset_number, cpu_model, \
                       cpu, memory, harddisk, sys_ip_inner, sys_ip_outer, environment, server_room_id, room_no_id, \
                       cabinet, idrac, occupy_u, start_cabinet, end_cabinet, start_time, maint_end_time, manufacturer_id, \
                       provider_id, comment, operator, responser, cursor):
    eq_update_sql = 'update equipment_info set equipment_type=' + str(equipment_type) + ',equipment_model="' + equipment_model \
                        + '",cpu_model="' + cpu_model + '",hostname="' + hostname + '",serial_num="' + serial_num \
                        + '",asset_number="' + asset_number + '",cpu="' + cpu + '",memory="' +  memory \
                        + '",harddisk="' + harddisk + '",sys_ip_inner="' + sys_ip_inner + '",sys_ip_outer="' + sys_ip_outer  \
                        + '",environment=' + str(environment) +  ',server_room_id=' +  str(server_room_id) \
                        + ',room_no_id=' + str(room_no_id) + ',cabinet="' + cabinet + '",occupy_u=' + str(occupy_u) \
                        + ',start_cabinet=' + str(start_cabinet) + ',end_cabinet=' + str(end_cabinet) \
                        +  ',idrac="' + idrac + '",start_time="' + start_time \
                        + '",maint_end_time="' + maint_end_time + '",manufacturer_id=' + str(manufacturer_id) \
                        + ',provider_id=' + str(provider_id) + ',comment="' + comment + '",responser="' + responser \
                        + '",operator="' + operator + '" where equipment_no="' + equipment_no + '"'
    cursor.execute(eq_update_sql)

def del_equipment_info(equipment_no, cursor):
    equipment_del = 'delete from equipment_info where equipment_no="' + equipment_no + '"'
    cursor.execute(equipment_del)


def exist_ip_equipment_info(sys_ip_inner):
    count = EquipmentInfo.objects.filter(sys_ip_inner=sys_ip_inner).count()
    if count > 0:
        return True
    return False

def exclude_ip_equipment_info(sys_ip_inner,id):
    count = EquipmentInfo.objects.filter(sys_ip_inner=sys_ip_inner).exclude(id=id).count()
    if count > 0:
        return True
    return False

def filter_host_equipment_info(hostname):
    count = EquipmentInfo.objects.filter(hostname=hostname).count()
    if count > 0:
        return True
    return False

def exclude_host_equipment_info(hostname, id):
    count = EquipmentInfo.objects.filter(hostname=hostname).exclude(id=id).count()
    if count > 0:
        return True
    return False

def get_host_equipment_info(hostname):
    count = EquipmentInfo.objects.filter(hostname=hostname).count()
    if count > 0:
        eRes = EquipmentInfo.objects.get(hostname=hostname)
        return eRes.equipment_no
    return False
